#include<stdio.h>
#include<stdlib.h>

struct student
{
	char stuname[20];
	char stunumber[15];
	int China;
	int math;
	int English;
}s[100];

void save(char *filename,int n)
{
		FILE *fp;
		int i;
		if((fp=fopen(filename,"wb+"))==NULL)
		{
			perror("fopen error!\n");
			exit(0);
		}
		for(i=0;i<n;i++)
		{
			//将一组数据写入到fp所指文件中
			if(fwrite(&s[i],sizeof(struct student),1,fp)!=1)//成功写入返回值为1(即count)
			{
				printf("file write error\n");
			}
		}
		fclose(fp);
}
void show(char *filename,int n)
{
	int i;
	FILE *fp;
	if((fp=fopen(filename,"rb"))==NULL)
	{
		perror("fopen error!\n");
		exit(0);
	}
	for(i=0;i<n;i++)
	{
		//从fp所指文件中读取数据到数组s中
		fread(&s[i],sizeof(struct student),1,fp);
		printf("%-10s%4s%4d%4d%4d\n",s[i].stuname,s[i].stunumber,s[i].China,s[i].math,s[i].English);
	}
	fclose(fp);
}

int main()
{
	int stunum;
	char filename[30];
	int i;
	printf("How many students in your class?\n");
	scanf("%d",&stunum);
	printf("please input filename:\n");
	scanf("%s",filename);
	printf("please input name,number,China,math,English:\n");
	for(i=0;i<stunum;i++)
	{
		printf("No%d\n",i+1);
		scanf("%s%s%d%d%d",s[i].stuname,s[i].stunumber,&s[i].China,&s[i].math,&s[i].English);
		save(filename,stunum);
	}
	show(filename,stunum);
	return 0;
}



